<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>原料验厂报告</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<div style="height: auto;width: 100%;text-align: center">
    <p style="font-size: 30px;color: #0081c2">原料验厂报告</p>
    <div style="width: 100%;height: auto;">
        <label style="font-size: 20px">物料批次号: </label>
        <input class="easyui-textbox" id="batchCode" style="width: 150px;height: 30px" required="required">
        <a href="#" class="easyui-linkbutton" style="width: 80px;height: 30px;margin-left: 10px"
           data-options="iconCls:'icon-search'" onclick="rawMaterialReport_btnRefresh()">查询</a>
        <a href="#" class="easyui-linkbutton" style="width: 100px;height: 30px;margin-left: 10px;"
           data-options="iconCls:'icon-print'" onclick="rawMaterialReport_preview()">打印预览</a>
        <a href="#" class="easyui-linkbutton" style="width: 100px;height: 30px;margin-left: 10px;"
           data-options="iconCls:'icon-print'" onclick="rawMaterialReport__print()">打印</a>
    </div>
    <!--    html5页面展示-->
    <!--    <div id="RawMaterialReport_Table" style="margin: 50px;margin-left: 380px"></div>-->

    <div style="margin: 50px;margin-left: 330px">
        <table id="RawMaterialReport_dg" class="easyui-datagrid" style="width:700px;height:auto"
               data-options="singleSelect: true,toolbar: '#RawMaterialReport_tb',onClickCell: onClickCell">
            <thead>
            <tr>
                <th data-options="field:'checkItem',width: 200, align: 'center',editor:'textbox'">检验项目</th>
                <th data-options="field:'standardValue',width: 240, align: 'center',editor:'textbox'">标准值</th>
                <th data-options="field:'actualValue',width:130,align: 'center',editor:'textbox'">实测值</th>
                <th data-options="field:'results',width:130,align: 'center',editor:'textbox'">结果评价</th>
            </tr>
            </thead>
        </table>
        <div id="RawMaterialReport_tb" style="height:auto">
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
               onclick="RawMaterialReport_add()">新增</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true"
               onclick="RawMaterialReport_removeit()">删除</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true"
               onclick="RawMaterialReport_save()">保存</a>
            <span style="color: red;margin-left: 20px">( 提示:修改后请先点击保存在执行打印 / 打印预览! )</span>
        </div>
    </div>
</div>


<script th:inline="none" type="text/javascript">
    var editIndex = undefined;

    //数据
    var rawMaterialReport_Json;
    //模板
    var rawMaterialReport_grfURL = "/grf/RawMaterialReport.grf";

    function endEditing() {
        if (editIndex == undefined) {
            return true
        }
        if ($('#RawMaterialReport_dg').datagrid('validateRow', editIndex)) {
            $('#RawMaterialReport_dg').datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }

    function onClickCell(index, field) {
        if (editIndex != index) {
            if (endEditing()) {
                $('#RawMaterialReport_dg').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                var ed = $('#RawMaterialReport_dg').datagrid('getEditor', {index: index, field: field});
                if (ed) {
                    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                }
                editIndex = index;
            } else {
                setTimeout(function () {
                    $('#RawMaterialReport_dg').datagrid('selectRow', editIndex);
                }, 0);
            }
        }
    }

    // function onEndEdit(index, row) {
    //     var ed = $(this).datagrid('getEditor', {
    //         index: index,
    //         field: 'checkItem'
    //     });
    //     row.productname = $(ed.target).combobox('getText');
    // }

    function RawMaterialReport_add() {
        var batchCode = $("#batchCode").val();
        if (batchCode != '' && batchCode != null) {
            if (endEditing()) {
                $('#RawMaterialReport_dg').datagrid('appendRow', {status: 'P'});
                editIndex = $('#RawMaterialReport_dg').datagrid('getRows').length - 1;
                $('#RawMaterialReport_dg').datagrid('selectRow', editIndex)
                    .datagrid('beginEdit', editIndex);
            }
        } else {
            topCommonMessage("请先输入物料批次号->查询!")
        }
    }

    function RawMaterialReport_removeit() {
        if (editIndex == undefined) {
            return
        }
        $('#RawMaterialReport_dg').datagrid('cancelEdit', editIndex)
            .datagrid('deleteRow', editIndex);
        editIndex = undefined;
    }

    function RawMaterialReport_save() {
        if (endEditing()) {
            var rows = $("#RawMaterialReport_dg").datagrid("getRows");
            $('#RawMaterialReport_dg').datagrid('acceptChanges');
            var batchCode = $("#batchCode").val();
            if (batchCode != '' && batchCode != null) {
                $.ajax({
                    type: "POST",
                    dataType: 'json',
                    contentType: "application/json",
                    url: "/qualityControl/RawMaterialReport?batchCode=" + batchCode,
                    data: JSON.stringify(rows),
                    success: function (data) {
                        if (data.status == 200) {
                            rawMaterialReport_Json = data;
                            $.messager.show({
                                title: '系统消息',
                                msg: "检验结果保存成功!",
                                timeout: 5000,
                                showType: 'slide'
                            });
                        } else if (data.status == 404) {
                            warnSessionConfirm();
                        }
                    }
                });
                console.log(rows);
            } else {
                topCommonMessage("请先输入物料批次号->查询!")
            }
        }
    }


    $(function () {
        //*这句特别重要*：只有在WEB报表客户端程序后，才能进行WebSocket通讯
        webapp_ws_autoupdate();
    })


    //根据数据过滤参数查询显示报表-->
    function rawMaterialReport_btnRefresh() {
        var BatchCode = $("#batchCode").val();
        if (BatchCode != null && BatchCode != '') {
            // HTML5页面展示
            // var RawMaterialReport_Table;
            //
            // RawMaterialReport_Table = window.rubylong.grhtml5.insertReportViewer("RawMaterialReport_Table", rawMaterialReport_grfURL, "");
            //
            // RawMaterialReport_Table.stop();
            //
            // rawMaterialReport_dataURL = encodeURI(rawMaterialReport_dataURL + BatchCode);
            //
            // RawMaterialReport_Table.dataURL = rawMaterialReport_dataURL;
            //
            // RawMaterialReport_Table.start();
            $('#RawMaterialReport_dg').datagrid({
                url: "/qualityControl/getCheckoutResult?batchCode=" + BatchCode,
                onLoadSuccess: function (data) {
                    if (data == null || data.total == 0) {
                        topCommonMessage("没有相关的数据!")
                    }
                }
            })
        } else topCommonMessage("请输入物料批次号!")
    }


    //消息响应函数，接受WEB报表客户端执行完打印与数据导出等任务回发的消息
    function webapp_onmessage(event) {
        var message = JSON.parse(event.data);

        //在向打印机发送完打印数据后响应的消息，在函数体中完成自己的任务
        function OnPrintEnd() {
            var msg = "报表已经打印，输出打印机：" + message.PrinterName;
            if (message.reportid) {
                msg += "\r\nreportid = " + message.reportid;
            }
            alert(msg);
        }

        //在执行了数据导出任务后响应的消息，在函数体中完成自己的任务
        function OnExportEnd() {
            var msg = "数据导出已经完成，导出文件：" + message.FileName;
            if (message.reportid) {
                msg += "\r\nreportid = " + message.reportid;
            }
            alert(msg);
        }

        //alert(event.data);
        if (message.event == "PrintEnd") {
            OnPrintEnd();
        } else if (message.event == "ExportEnd") {
            OnExportEnd();
        }
    }

    //打印预览
    function rawMaterialReport_preview() {
        if (rawMaterialReport_Json != null && rawMaterialReport_Json != '') {
            var option = {
                report: urlAddRandomNo(rawMaterialReport_grfURL),
                data: rawMaterialReport_Json
            }
            webapp_ws_ajax_run(option);
        } else {
            topCommonMessage("请先点击保存再执行打印预览!")
        }
    }

    function rawMaterialReport__print() {
        if (rawMaterialReport_Json != null) {
            var option = {
                type: "print",
                //showOptionDlg: false, //如果不显示打印对话框而直接打印，将此行注释去掉即可
                report: urlAddRandomNo(rawMaterialReport_grfURL),
                data: rawMaterialReport_Json
            }
            webapp_ws_ajax_run(option);
        } else {
            topCommonMessage("请先点击保存再执行打印!")
        }
    }
</script>
</body>
</html>